# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import re

class ParseConfig(object):
    '''读取配置'''
    def __init__(self, content):
        super(ParseConfig, self).__init__()
        self.content = content
        self.configs = {
            'paragraph_class': 'p-style',
            'paragraph_tag': 'p'
        }

    def read(self):
        '''读取配置'''
        _arr = self.content.split('\n')
        _i = 0
        for _line in _arr:
            if _line.startswith('@'):
                _i += 1
                self._analyze(_line)
        self.content = '\n'.join(_arr[_i:]).strip()
        return self.configs

    def _analyze(self, _line):
        _tmp = re.sub(r'^@[^:]+:', '', _line)
        _key = _line[1:-len(_tmp)][:-1]
        _val = _tmp.strip()
        if not bool(_val):
            _val = None
        _keys = self.configs.keys()
        if _key in _keys:
            self.configs[_key] = _val